﻿#region Copyright 
/*-------------------------------------------------------------------------
* 命名空间名称/文件名:    SSPivot.AspNetCore.Repository.UnitOfWork/CapTransaction 
* CLR版本:     4.0.30319.42000
* 机器名称:    DESKTOP-NHMLO8A
* 功 能：       N/A
* 类 名：       CapTransaction
* 创建时间：  2025/6/3 14:44:31
* 版本号：      v1.0
* 创建人:        xulong
*-------------------------------------------------------------------------*/
#endregion
using DotNetCore.CAP;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SSPivot.AspNetCore.Repository
{
    public class CapTransaction<TDbContext> : ICapTransaction where TDbContext:DbContext
    {
        private readonly IDbContextTransaction _transaction;
        public CapTransaction(AppDbContext<TDbContext> context, ICapPublisher publisher)
        {
            _transaction = context.Database.BeginTransaction(publisher);
        }

        public object DbTransaction => _transaction.GetDbTransaction();

        public bool AutoCommit { get; set ; }
        object? ICapTransaction.DbTransaction { get; set; }

        public void Commit() => _transaction.Commit();
        public void Rollback() => _transaction.Rollback();
        public void Dispose() => _transaction.Dispose();

        public async Task CommitAsync(CancellationToken cancellationToken = default)
        {
           await  _transaction.CommitAsync(cancellationToken);
        }

        public async Task RollbackAsync(CancellationToken cancellationToken = default)
        {
           await _transaction.RollbackAsync(cancellationToken);
        }
    }
}
